package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.rewardcoin.MirrorInfo;
import com.zhongxi.guoyuan.entity.rewardcoin.RewardCoinOrderResult;
import com.zhongxi.guoyuan.url.Url;

import org.json.JSONArray;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/3/22.
 */

public class RewardOrderAdapter extends BaseAdapter {
    private List<MirrorInfo> data;
    private Context context;

    public RewardOrderAdapter(Context context, List<MirrorInfo> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        if (data.size() > 0) {
            return data.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try {
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_order_list, parent, false);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                holder.tvAttr = (TextView) convertView.findViewById(R.id.tvAttr);
                holder.tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
                holder.tvNum = (TextView) convertView.findViewById(R.id.tvNum);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            MirrorInfo mirrorInfo = data.get(position);
            Glide.with(context).load(Url.IMG_URL + mirrorInfo.getThumbnail()).placeholder(R.mipmap.iv_eg).fitCenter().into(holder.ivImg);
            holder.tvName.setText(mirrorInfo.getGoodsname());
            holder.tvPrice.setText(mirrorInfo.getGoodsunitprice() + " 元");
            holder.tvNum.setText("X " + mirrorInfo.getGoodsnum());
            JSONObject jsonObject = new JSONObject(mirrorInfo.getGattr());
            Iterator<String> keys = jsonObject.keys();
            String attr = "";
            while (keys.hasNext()) {
                String next = keys.next();
                String attrValue = jsonObject.optString(next);
                attr += attrValue + "/";
            }
            holder.tvAttr.setText(attr.substring(0, attr.length() - 1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder {
        private ImageView ivImg;
        private TextView tvName;
        private TextView tvAttr;
        private TextView tvPrice;
        private TextView tvNum;
    }
}
